<template>
  <a-card :bordered="false">

    <div class="table-operator">
      <a-button type="primary" icon="setting" @click="handleSetting" >设置</a-button>
    </div>

    <!-- <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="{ show: true, clear: () => { this.selectedRowKeys = [] } }"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >
    

    </s-table> -->
     <a-table :columns="columns" :dataSource="loadData">
   
  
  </a-table>

    <a-modal
      title="设置"
      :width="800"
      v-model="visible"
      @ok="handleOk"
    >
      <a-form :form="form">
           <a-form-item
           label="上半学期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-range-picker   
        v-model="schoolTimeData.upDate"
    />
        </a-form-item>
        <a-form-item
         label="下半学期"
         :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-range-picker
               v-model="schoolTimeData.downDate"

    />
        </a-form-item>

      </a-form>
    </a-modal>

  </a-card>
</template>

<script>
  import STable from '@/components/table/'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import AInput from 'ant-design-vue/es/input/Input'
  import moment from 'moment'

  import { getRoleList, getGradeList, getSchoolTime, updateSchoolTime} from '@/api/manage'

  export default {
    name: 'GradeList',
    components: {
      AInput,
      ATextarea,
      STable
    },
    data () {
      return {
        visible: false,
//        description: '全校班级管理。',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 12 },
        },
        form: this.$form.createForm(this),
        mdl: {},

        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '年级',
            dataIndex: 'gradeName'
          },
          {
            title: '入学年级',
            dataIndex: 'year'
          },
          {
            title: '班级数',
            dataIndex: 'clsCount'
          },
          {
            title: '年级总人数',
            dataIndex: 'studentCount',
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        // loadData: parameter => {
        //   return getGradeList(Object.assign(parameter, this.queryParam))
        //     .then(res => {
        //       return res.result
        //     })
        // },
        loadData: null,
        selectedRowKeys: [],
         selectedRows: [],
      schoolTimeData: {
        upDate: [],
        downDate: []
      },
      saveIdData: {
        saveUpDateId: null,
         saveDownDateId: null,
      }
      }
    },
    created () {
      getRoleList({ t: new Date()})
      let self = this;
      getGradeList().then(res => {
             self.loadData = res.result
        })
    },
    methods: {

   

      handleSetting () {
        this.visible = true
        let self = this
        this.schoolTimeData.upDate = []
        this.schoolTimeData.downDate = []
        getSchoolTime({name: "上半学期"}).then(res => {
          self.schoolTimeData.upDate.push( moment(new Date(res.result.valueOne)) ,moment(new Date(res.result.valueTwo)))
          self.saveIdData.saveUpDateId = res.result.id
        
     console.log(self.getSchoolTimeData)
        })
         getSchoolTime({name: "下半学期"}).then(res => {
            self.saveIdData.saveDownDateId = res.result.id
   self.schoolTimeData.downDate.push( moment(new Date(res.result.valueOne)) ,moment(new Date(res.result.valueTwo)))  
        })
         
      },
      handleOk () {
       console.log(this.schoolTimeData) 
       let self = this  
         updateSchoolTime({id: this.saveIdData.saveUpDateId, name: "上半学期", 
          valueOne: this.schoolTimeData.upDate[0].format('YYYY-MM-DD'),
          valueTwo: this.schoolTimeData.upDate[1].format('YYYY-MM-DD') })
         .then(res => {
           console.log(res.success)
         })
          updateSchoolTime({id: this.saveIdData.saveDownDateId, name: "下半学期", 
          valueOne: this.schoolTimeData.downDate[0].format('YYYY-MM-DD'),
          valueTwo: this.schoolTimeData.downDate[1].format('YYYY-MM-DD')})
          .then(res => {
           console.log(res.success)
         })
           this.visible = false
      },

      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },

      resetSearchForm () {
        this.queryParam = {
          date: moment(new Date())
        }
      }
    },
    watch: {
      /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
    }
  }
</script>